package com.baibei.sdk;

import android.util.Log;

import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.ITokenApi;
import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.model.UserInfo;
import com.github.raee.runit.AndroidRUnit4ClassRunner;
import com.rae.swift.session.SessionManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import retrofit2.Response;

/**
 * token 测试
 * Created by ChenRui on 2017/8/3 0003 10:54.
 */
@RunWith(AndroidRUnit4ClassRunner.class)
public class TokenApiTest extends SdkTest {

    private ITokenApi mTokenApi;

    @Override
    @Before
    public void setup() {
        super.setup();
        mTokenApi = getApiProvider().getTokenApi();
    }

    @Test
    public void testAppKey() throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response<Empty> response = null;
                try {
                    response = mTokenApi.getAppKey().execute();
                    Log.i("Rae", "结果：" + response.code() + " --> " + response.message());
                    AndroidRUnit4ClassRunner.finish("testAppKey");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Test
    public void testRefreshToken() throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<Empty> execute = mTokenApi.getAppKey().execute();
                    ApiFactory.getInstance().getUserApi().signIn("15918716301", "123456")
                            .subscribe(new Consumer<UserInfo>() {
                                @Override
                                public void accept(@NonNull UserInfo userInfo) throws Exception {
                                    Response<TokenInfo> response = null;
                                    String token = ((TokenInfo) SessionManager.getDefault().getUserToken()).getRefreshToken();
                                    response = mTokenApi.refreshToken(token).execute();
                                    Log.i("Rae", "结果：" + response.body().getAccessToken() + " --> " + response.message());
                                }
                            });
//                    SessionManager.getDefault().setUserToken(new TokenInfo("E9ZW720VW2KYW8G82ED3YMG9A4SY8QSD","UDJHDF0BSATGO0TD0GLNLLXFHFNITSB2"));

                } catch (IOException e) {
                    e.printStackTrace();
                }
                AndroidRUnit4ClassRunner.finish("testRefreshToken");
            }
        }).start();
    }
}
